package com.example.trb.service.education;

import com.example.trb.dto.ObjectDTO;
import com.example.trb.entity.Education;
import com.example.trb.mapper.EducationMapper;
import com.example.trb.mapper.UserMapper;
import com.example.trb.util.UrlUtil;
import com.example.trb.util.result.SysReturnCode;
import com.example.trb.vo.EducationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EducationService {
    @Autowired
    EducationMapper educationMapper;
    @Autowired
    UserMapper userMapper;
    public ObjectDTO updateStatusById(Integer id, String status) {
        educationMapper.updateStatusById(id, status);
        return new ObjectDTO(null, SysReturnCode.OK);
    }

    public ObjectDTO updateById(EducationVO vo) {
        educationMapper.updateById(new Education(
                vo.getId(), null, vo.getGraduationTime(),
                vo.getSchool(), vo.getSpecialty(), vo.getAcademicStructure(),
                vo.getEducationNumber(), vo.getDegree(), UrlUtil.urlsToString(vo.getMaterials()),null
        ));

        ObjectDTO dto = new ObjectDTO();
        dto.setCode(SysReturnCode.OK);
        return dto;
    }

    public ObjectDTO removeById(Integer id) {
        educationMapper.deleteById(id);
        return new ObjectDTO(null, SysReturnCode.OK);
    }

    public ObjectDTO listByUserId(Integer userId) {
        ObjectDTO dto = new ObjectDTO();

        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("user_id", userId);

        List<Education> r = educationMapper.selectByMap(queryMap);
        List<EducationVO> results = r.stream()
                .map(education -> {
                    return new EducationVO(
                            education.getId(), education.getGraduationTime(),
                            education.getSchool(), education.getSpecialty(),
                            education.getAcademicStructure(), education.getEducationNumber(),
                            education.getDegree(), education.getMaterials().split(";")
                    );
                })
                .collect(Collectors.toList());

        dto.setCode(SysReturnCode.OK);
        dto.setData(results);
        return dto;
    }

    public ObjectDTO getById(Integer id) {
        Education result = educationMapper.selectById(id);
        ObjectDTO dto = new ObjectDTO();
        dto.setCode(SysReturnCode.OK);
        dto.setData(result);
        return dto;
    }

    public ObjectDTO save(EducationVO vo, Integer userId) throws ParseException {
        ObjectDTO dto = new ObjectDTO();

        Education education = new Education(
                null, userId, vo.getGraduationTime(),
                vo.getSchool(), vo.getSpecialty(), vo.getAcademicStructure(),
                vo.getEducationNumber(), vo.getDegree(), UrlUtil.urlsToString(vo.getMaterials()),null
        );
        educationMapper.insert(education);

        dto.setCode(SysReturnCode.OK);
        dto.setData(education.getId());
        return dto;
    }
}
